﻿using MLRobotic.Tower.Detection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Drawing.Imaging;
using System.Drawing;
using System.Collections.Generic;
using System;
using MLRobotic.Tower.Common;

namespace MLRobotic.Tower.Test
{
    [TestClass()]
    public class ImageProcessorTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            Log.LogFolder = "";
        }
        
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        [DeploymentItem(@"Data", "Data")]
        public void Find_TestSalon()
        {
            string name = "test_salon";
            var image = new Bitmap(@"Data\" + name + ".jpg");

            Config config = new Config(Phone.TouchPro2) { Width = image.Width, Height = image.Height };

            double hue, saturation, brightness;
            HsvHelper.ColorToHSL(80, 10, 10, out hue, out saturation, out brightness);

            var targetColor = new TargetColor(Color.Red, hue, config.PixelFormat);
            targetColor.MinSaturation = 0.1;
            targetColor.MinBrightness = 0.1;

            ImageProcessor processor = new ImageProcessor(config);
            processor.SetImage(image);

            var actual = processor.Find(new List<TargetColor> { targetColor }, new Rectangle(0, 0, image.Width, image.Height), true);

            var result = processor.LastResult;

            result.Save(name + "_result.jpg", ImageFormat.Jpeg);
        }

        [TestMethod()]
        [DeploymentItem(@"Data", "Data")]
        public void Find_TestGrandAngleSansFocus()
        {
            string name = "test_grand_angle_sans_focus";
            var image = new Bitmap(@"Data\" + name + ".jpg");

            Config config = new Config(Phone.TouchPro2) { Width = image.Width, Height = image.Height };

            var red = new TargetColor(Color.Red, config.PixelFormat);
            red.SetColor(115, 60, 50);
            red.DeltaHue = 20;
            red.MinBrightness = 0.1;
            red.MinSaturation = 0.5;

            var blue = new TargetColor(Color.Blue, config.PixelFormat);
            blue.SetColor(32, 51, 22);
            blue.DeltaHue = 50;
            blue.MinBrightness = 0.1;
            blue.MinSaturation = 0.4;

            ImageProcessor processor = new ImageProcessor(config);
            processor.SetImage(image);

            var actual = processor.FindCandlesBySplittingArea(true);

            var result = processor.LastResult;

            result.Save(name + "_result.jpg", ImageFormat.Jpeg);
        }
    }
}
